package com.cby.operation.initialize.ui

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.text.*
import android.text.style.ForegroundColorSpan
import android.text.style.URLSpan
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.cby.operation.common.ext.getId
import com.cby.operation.common.ui.BaseDialogFragment
import com.cby.operation.common.util.StringUtils
import com.cby.operation.common.util.WebUtils
import com.cby.operation.common.widget.richtext.BetterLinkMovementMethod
import com.cby.operation.common.widget.richtext.HtmlTagHandler
import com.cby.operation.common.widget.richtext.LineSpaceExtraTextView
import com.cby.operation.initialize.InitManager
import com.cby.operation.sdk.ERR_CODE_EXIT_GAME
import com.cby.operation.sdk.ERR_MSG_EXIT_GAME
import com.cby.operation.sdk.TAG_FRAGMENT_GAME_MAINTENANCE

/**
 * @description: MaintenanceDialogFragment
 *
 * @author ChenBaoyang
 * @date 7/22/21 10:14
 */
class MaintenanceDialogFragment : BaseDialogFragment() {



    private var content: String? = null

    private var maintenanceContentTv: LineSpaceExtraTextView? = null
    private var negativeBtn: Button? = null

    companion object{
        private const val KEY_MAINTENANCE_CONTENT = "maintenance_content"

        fun show(activity: Activity, content: String) : BaseDialogFragment {
            val fragment = MaintenanceDialogFragment()
            val data = Bundle()
            data.putString(KEY_MAINTENANCE_CONTENT, content)
            fragment.arguments = data
            fragment.show(activity.fragmentManager, TAG_FRAGMENT_GAME_MAINTENANCE)

            return fragment
        }
    }


    override fun parseArgs() {
        content = arguments.getString(KEY_MAINTENANCE_CONTENT)
    }

    override fun layoutName() = "dialog_fragment_maintenance"

    override fun initView(view: View?) {
        maintenanceContentTv = view!!.findViewById(getId("tv_maintenance_content"))
        negativeBtn = view.findViewById(getId("dlg_btn_negative"))
        //去掉点击链接的背景
        maintenanceContentTv!!.highlightColor = Color.parseColor("#00000000")
    }

    override fun setContent() {
        if (!TextUtils.isEmpty(content)) {
            val betterLinkMovementMethod: BetterLinkMovementMethod = BetterLinkMovementMethod.linkifyHtml(maintenanceContentTv!!)
                    .setOnLinkClickListener(
                            object : BetterLinkMovementMethod.OnLinkClickListener {
                                override fun onClick(textView: TextView?, url: String?): Boolean {
                                    //增加温馨提示的链接点击统计
                                    if (!StringUtils.isValidHttpUrl(url)) {
                                        return true
                                    }

                                    //打开url
                                    WebUtils.openUrl(textView!!.context, url!!)
                                    return true
                                }
                            })
            betterLinkMovementMethod.removeUrlHighlightColor(maintenanceContentTv!!)
            val spanned = Html.fromHtml(content, null,
                    HtmlTagHandler(null))
            maintenanceContentTv!!.setCustomText(getHtmlUrlSpanStyle(spanned))
            maintenanceContentTv!!.movementMethod = betterLinkMovementMethod
        }
    }

    /**
     * 设置a标签颜色
     */
    private fun getHtmlUrlSpanStyle(spanned: Spanned): SpannableStringBuilder? {
        val htmlBuilder = SpannableStringBuilder(spanned)
        val urls = htmlBuilder.getSpans(0,
                spanned.length, URLSpan::class.java)
        //超链接颜色
        val color = Color.parseColor("#23c268")
        for (urlSpan in urls) {
            val start = htmlBuilder.getSpanStart(urlSpan)
            val end = htmlBuilder.getSpanEnd(urlSpan)
            htmlBuilder.setSpan(object : ForegroundColorSpan(color) {
                override fun updateDrawState(textPaint: TextPaint) {
                    super.updateDrawState(textPaint)
                    //去掉下划线，真正有效的地方
                    textPaint.isUnderlineText = false
                }
            }, start, end,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        }
        return htmlBuilder
    }

    override fun initListener() {
        negativeBtn!!.setOnClickListener {
            // 退出游戏, 回调给游戏自己去做
            dismiss()
            //回调给游戏开发，让游戏去做退出游戏
            InitManager.instance.handleInitFailedCallBack(ERR_CODE_EXIT_GAME, ERR_MSG_EXIT_GAME)
        }
    }
}